const Express = require("express");
const NewServer = require("../../servers/news");
const ResponseHelper = require("../ResponseHelper");
const router = Express.Router();

router.get("/newsAll", async (req, res) => {
  try {
    const result = await NewServer.getAllNews();
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});
router.get("/getNewsList", async (req, res) => {
  try {
    const { page, limit, title, publishTime, publishName, type } = req.query;
    const result = await NewServer.find(
      page,
      limit,
      type ? type : null,
      title ? title : null,
      publishTime ? publishTime : null,
      publishName ? publishName : null
    );
    ResponseHelper.sendData(result, res);
  } catch (err) {
    console.log("err", err);
    ResponseHelper.sendErr(err, res);
  }
});
router.post("/addNew", async (req, res) => {
  try {
    const result = await NewServer.addNews(req.body);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    console.log("err", err);
    ResponseHelper.sendErr(err, res);
  }
});
router.post("/updateNews/:id", async (req, res) => {
  const id = req.params.id;
  const newsObj = req.body;
  try {
    const result = await NewServer.updateNews(id, newsObj);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});
router.get("/getNew/:id", async (req, res) => {
  try {
    const id = req.params.id;
    const result = await NewServer.getNewsById(id);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});
router.get("/delNew/:id", async (req, res) => {
  try {
    const id = req.params.id;
    const result = await NewServer.delNew(id);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});
module.exports = router;
